package com.esd.csair;

import java.io.IOException;
import java.io.Serializable;
import java.net.URL;

import org.apache.log4j.Logger;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.FalsifyingWebConnection;

public class SimpleConectionListener implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8168493020295282906L;



	private static Logger log = Logger.getLogger(SimpleConectionListener.class);

	public String URLloginFunction = null;
	public String URLPassengerInformation = null;
	public String URLorderQuery = null;
	public String URLcsairlogin = null;
	// public String URLCsairbaseInfo = null;
	public String URLpay = null;
	public String URLpayComplete = null;
	public String URLpayErro = null;
	public String URLcsairConnectInfoLoad = null;
	public String URLcsairConnectInfojs = null;
	public String URLaddressAction = null;
	public String URLdropdownAction = null;
	public String URLsaveskypearlAction = null;
	public String URLModifyPwdcheck = null;
	public String URLorderDetail = null;
	public String URLOrderMXError = null;
	public String URLAssigneeCtrl = null;
	public String URLAssigneeCtrljs = null;
	public String URLCheckYzm = null;
	public String URLorderCN = null;
	public String URLflight = null;



	public String BJLHLoginSuccess = null;
	public String BJLHxsjxpjReturnPage = null;
	
	
	public SimpleConectionListener() {}
	
	public SimpleConectionListener(WebClient webClient) {
		FalsifyingWebConnection fa = new FalsifyingWebConnection(webClient) {
			@Override
			protected WebResponse createWebResponse(WebRequest wr, String content, String contentType) throws IOException {

				return super.createWebResponse(wr, content, contentType);
			}
			@Override
			protected WebResponse createWebResponse(WebRequest wr, String content, String contentType, int responseCode,
					String responseMessage) throws IOException {
				log.info("=======================createWebResponse=================================================");
				log.info("[content:" + content + "][contentType:" + contentType + "]" + "[responseCode:" + responseCode + "]"
						+ "[responseMessage:" + responseMessage + "]");
				return super.createWebResponse(wr, content, contentType, responseCode, responseMessage);
			}

			@Override
			protected WebResponse replaceContent(WebResponse wr, String newContent) throws IOException {
				log.info("=======================replaceContent=================================================");
				log.info("[wr:" + wr + "][newContent" + newContent + "]");
				return super.replaceContent(wr, newContent);
			}

			@Override
			protected WebResponse deliverFromAlternateUrl(WebRequest webRequest, URL url) throws IOException {
				log.info("=======================deliverFromAlternateUrl=================================================");
				log.info("[webRequest:" + webRequest + "][url" + url + "]");
				return super.deliverFromAlternateUrl(webRequest, url);
			}
			
			@Override
			public WebResponse getResponse(WebRequest request) throws IOException {
				WebResponse response = super.getResponse(request);
				String url = response.getWebRequest().getUrl().toString();
				long time = response.getLoadTime();
				String type = response.getContentType();
				int statusCode = response.getStatusCode();
				String msg = response.getStatusMessage();
				response.getWebRequest().getEncodingType();
				log.info("[url:" + url + "][time:" + time + "][type:" + type + "][statusCode:" + statusCode + "][msg:" + msg
						+ "]");

				if (url.endsWith("topMenu.js")) {
					return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
				}
				if (url.endsWith("DD_belatedPNG_0.0.7a-min.js")) {
					return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
				}
				
				if (url.endsWith("wtid.js")) {
					log.info("-------wtid.js-------");
					return createWebResponse(response.getWebRequest(), "", "application/x-javascript", 200, "Ok");
				}
				if (url.endsWith("sdc_gd.js")) {
					log.info("-------sdc_gd.js-------");
					return createWebResponse(response.getWebRequest(), "", "application/x-javascript", 200, "Ok");
				}
				if (url.endsWith("discountInfo")) {
					log.info("-------discountInfo-------");
					return createWebResponse(response.getWebRequest(), "", "application/json", 200, "Ok");
				}
				
				if (type.equals("text/css")) {
					
				}

				handle(url);
				return response;
			}

		};
		try {
			fa.close();
		} catch (Exception e) {
			log.error("fa====>Exception", e);
		}
	}
	
	
	
	
	public static void main(String[] args) {
		String path = System.getProperty("user.dir");
		System.out.println(path);
	}

	
	
	

	

	public void handle(String url) {
		
		if (url.startsWith(Csair.BJLHLoginSuccess)) {
			BJLHLoginSuccess = url;
		}
		if (url.startsWith(Csair.BJLHxsjxpjReturnPage)) {
			BJLHxsjxpjReturnPage = url;
		}
		// return false;
	}

	
}
